package net.kleinhenz.droidshop;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import net.kleinhenz.droidshop.R;
import net.kleinhenz.droidshop.model.ListEntry;
import net.kleinhenz.droidshop.model.Product;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.Toast;

public class DroidShopTabsActivity extends TabActivity implements TabHost.TabContentFactory 
{
	public static int MENUITEM_ADDWISHLIST = 0;
	public static int MENUITEM_WISHLIST = 1;
	public static int MENUITEM_HISTORY = 2;
	public static int MENUITEM_ABOUT = 3;
	public static int MENUITEM_SHARE = 4;

	private Product product = null;
	private static ListFactory listFactory = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		
		this.product = (Product)this.getIntent().getExtras().getSerializable("product");
		
		Intent productIntent = new Intent(this, ProductActivity.class);
		productIntent.putExtra("product", product);
		
		Intent pricesIntent = new Intent(this, PricesActivity.class);
		pricesIntent.putExtra("product", product);

		Intent reviewIntent = new Intent(this, ReviewActivity.class);
		reviewIntent.putExtra("product", product);

		final TabHost tabHost = getTabHost();
		
		LayoutInflater.from(this).inflate(R.layout.tabhost, tabHost.getTabContentView(), true);
		
		tabHost.addTab(tabHost.newTabSpec("product")
				.setIndicator(getResources().getString(R.string.product), getResources().getDrawable(android.R.drawable.ic_dialog_info))
				.setContent(productIntent));

		tabHost.addTab(tabHost.newTabSpec("prices")
				.setIndicator(getResources().getString(R.string.prices), getResources().getDrawable(android.R.drawable.ic_dialog_alert))
				.setContent(pricesIntent));

		tabHost.addTab(tabHost.newTabSpec("reviews")
				.setIndicator(getResources().getString(R.string.reviews), getResources().getDrawable(android.R.drawable.ic_dialog_email))
				.setContent(reviewIntent));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
		menu.clear();
	    menu.add(0, MENUITEM_HISTORY, 0, R.string.menu_history)
    	.setIcon(android.R.drawable.ic_menu_compass);
	    menu.add(0, MENUITEM_WISHLIST, 1, R.string.menu_wishlist)
        .setIcon(android.R.drawable.ic_menu_agenda);
	    menu.add(0, MENUITEM_ADDWISHLIST, 2, R.string.menu_addwishlist)
        .setIcon(android.R.drawable.ic_menu_add);
	    menu.add(0, MENUITEM_SHARE, 3, R.string.menu_share)
        .setIcon(android.R.drawable.ic_menu_share);
	    return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) 
	{
		if (item.getItemId()==MENUITEM_ADDWISHLIST)
		{
			addToList(this, ListFactory.LIST_WISHLIST, this.product);
			Toast.makeText(this, getResources().getString(R.string.product_added), Toast.LENGTH_SHORT).show(); 
		}	
		else if (item.getItemId()==MENUITEM_HISTORY)
		{
			Intent listIntent = new Intent(this, ProductListActivity.class);			
			listIntent.putExtra("list", ListFactory.LIST_HISTORY);
			startActivity(listIntent);			
		}
		else if (item.getItemId()==MENUITEM_WISHLIST)
		{
			Intent listIntent = new Intent(this, ProductListActivity.class);
			listIntent.putExtra("list", ListFactory.LIST_WISHLIST);
			startActivity(listIntent);			
		}	
		else if (item.getItemId()==MENUITEM_SHARE)
		{
			//String link = getResources().getString(R.string.share_link) + product.getEan();
			String link = shortenLink(product.getDetailLink());
			String text = getResources().getString(R.string.share_text).replace("%LINK%", link);
			
			Intent sendIntent = new Intent(Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT, text); 
			sendIntent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject)); 
			sendIntent.setType("text/plain");

           	this.startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.share_with)));
		}	
		return true;
	}

	@Override
	public View createTabContent(String tag) 
	{
		return null;
	}
	
	public static String shortenLink(URL detailLink)
	{
		try
		{
			URL url = new URL("http://tinyurl.com/api-create.php?url=" + detailLink.toString());
			InputStream is = url.openStream();
			StringBuffer tinyUrl = new StringBuffer(); 
			int data = 0;
			while ((data=is.read())!=-1)
				tinyUrl.append((char)data);
			return tinyUrl.toString();
		}
		catch (MalformedURLException e)
		{
			return detailLink.toString();
		}
		catch (IOException e)
		{
			return detailLink.toString();
		}
	}

	public static void addToList(Context context, String list, Product product) 
	{
		ListEntry entry = new ListEntry();
		entry.setKeywords(product.getAsin());
		entry.setDate(new Date());
		entry.setTitle(product.getName());
		entry.setType(product.getType());
		entry.setAuthor(product.getAuthor());
		entry.setImageUrl(product.getImageLink().toString());
		entry.setDetailUrl(product.getDetailLink().toString());
		getListFactory(context).addToList(list, entry);
	}

	private static ListFactory getListFactory(Context context)
	{
		if (listFactory ==null)
			listFactory = new ListFactory(context);
		return listFactory;
	}	
}
